package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Storage;
import com.abaaba.project2.bean.StorageExample;
import com.abaaba.project2.bean.bo.StorageListBo;
import com.abaaba.project2.bean.vo.StorageListVo;
import com.abaaba.project2.mapper.StorageMapper;
import com.abaaba.project2.service.AdminStorageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class AdminStorageServiceImpl implements AdminStorageService {
    @Autowired
    StorageMapper storageMapper;

    @Override
    public StorageListVo getStorageList(StorageListBo storageListBo) {

        //分页设置
        PageHelper.startPage(storageListBo.getPage(),storageListBo.getLimit());
        //Bo封装example
        StorageExample storageExample = new StorageExample();
        storageExample.setOrderByClause(storageListBo.getSort() + " " + storageListBo.getOrder());
        StorageExample.Criteria criteria = storageExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        if(storageListBo.getKey() != null && !"".equals(storageListBo.getKey()))
        {
            criteria.andKeyLike("%"+storageListBo.getKey()+"%");
        }
        if (storageListBo.getName() != null && !"".equals(storageListBo.getName()))
        {
            criteria.andNameLike("%"+storageListBo.getName()+"%");
        }

        //调用Dao层
        List<Storage> storages = storageMapper.selectByExample(storageExample);

        return new StorageListVo((int)new PageInfo<Storage>(storages).getTotal(),storages);
    }

    @Override
    public Integer storageCreate(Storage storage) {

        StorageExample storageExample = new StorageExample();
        StorageExample.Criteria criteria = storageExample.createCriteria();

        storageMapper.insertSelective(storage);

        criteria.andKeyEqualTo(storage.getKey());
        List<Storage> storages = storageMapper.selectByExample(storageExample);

        Storage storage1 = storages.get(0);

        return storage1.getId();
    }

    @Override
    public void storageUpdate(Storage storage) {
        Date date = new Date(System.currentTimeMillis());
        storage.setUpdateTime(date);

        StorageExample storageExample = new StorageExample();
        StorageExample.Criteria criteria = storageExample.createCriteria();
        criteria.andIdEqualTo(storage.getId());

        storageMapper.updateByExampleSelective(storage,storageExample);

    }

    @Override
    public void storageDelete(Storage storage) {
        //删除要使使用这个图片的对象不能再用了，那就物理删除算了
        Storage deleteMark = new Storage();
        deleteMark.setDeleted(true);
        StorageExample storageExample = new StorageExample();
        StorageExample.Criteria criteria = storageExample.createCriteria();
        criteria.andIdEqualTo(storage.getId());

        storageMapper.updateByExampleSelective(deleteMark,storageExample);

        String filePath = "D://project2/"+storage.getKey();
        //物理删除图片
        File file = new File(filePath);
        file.delete();
    }
}
